package exe.jvmbytecodes;
import java.io.IOException;
import java.lang.InterruptedException;
import java.io.File;
import java.io.FilenameFilter;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;


import java.io.*;
import java.util.*;
import java.net.*;

import org.jdom.*;

import exe.*;
import exe.pseudocode.*;



 class BytecodeViewer { 

	static final String TITLE = null;   
	static final String FILE = "exe/jvmbytecodes/template.xml";
	static final String CLASSFILE = "exe/jvmbytecodes/classTemplate.xml";

	static PseudoCodeDisplay pseudo;
   	static PseudoCodeDisplay realCode;
    	static ShowFile show;
	static int numberOfLinesInJavaFile=1;
        static int stackSize=0;
        static int heapSize=0;
	static int currentMethod = 1;	
	static GAIGSstack runTimeStack;
	static GAIGSstack heap;
	static ArrayList _heap;
	static int currentClass;
	int questionID;
	static MakeURI muri = new MakeURI();
	static GenerateBytecodes gbc = new GenerateBytecodes();
    
    	public static void main(String args[])  throws IOException {
		BytecodeViewer bcv = new BytecodeViewer();
		bcv.runViewer(args);
   
   	 }
	
	
	public void runViewer(String a[]) throws IOException
	{
		show = new ShowFile(a[0],5); //first argument is the script foo.sho
		
    		//generate the bytecodes
		//String[] temp = {"../../src/exe/jvmbytecode/Test","Factorial4.java"};
		String[] temp = { a[1] , a[2] } ;
		gbc.run(temp);
		
		//create visual run time stack and heap using the predetermined sizes
		heap = new GAIGSstack("Heap", "#999999", 0.01,0.5, 0.3, 0.9, 0.15);
		runTimeStack = new GAIGSstack("Run Time Stack", "#999999", 0.01,0.1,0.3, 0.5, 0.15);

		//new xml file		
		currentClass=0; 
		GenerateXML.generateXMLfile();	
		GenerateXML.generateJavaXMLFile(a[1],a[2]);
		
        	try {
		    pseudo = new PseudoCodeDisplay(FILE);
		    realCode = new PseudoCodeDisplay(CLASSFILE);
		} 
		catch (JDOMException e) {
		    e.printStackTrace();
		}
		
		Frame_ f = new Frame_();

		InitializeViz iv = new InitializeViz();
		iv.firstMethodCall();
		show.writeSnap(TITLE, muri.doc_uri(-1), muri.make_uri(-1, pseudo.RED), heap, runTimeStack);
		show.writeSnap(TITLE, muri.doc_uri(-1), muri.make_uri(-1, pseudo.RED), heap, runTimeStack, f.stack, f.localVariableArray);

		//begin interpreter
		Interpreter interp = new Interpreter();
		interp.interpret(gbc.classes[0].methods.get(1).bytecodes, f);
		
		runTimeStack.pop();
		show.writeSnap(TITLE, muri.doc_uri(-1), muri.make_uri(-1, pseudo.RED), heap, runTimeStack);
		show.close();
	}

 	static String getRandomColor() {
 		String returnStr="#";
 		int temp=0;
 		for(int i=0; i<6; i++) {
 			Random rand = new Random();
 			temp = rand.nextInt(7);
 			if (temp == 10)
 				returnStr += "a";
 			else if (temp == 11)
 				returnStr += "b";
 			else if (temp == 12)
 				returnStr += "c";
 			else if (temp == 13)
 				returnStr += "d";
 			else if (temp == 14)
 				returnStr += "e";
 			else if (temp == 15)
 				returnStr += "f";
 			else
 				returnStr += (Integer.toString(temp));
 		}
 		return (returnStr);
 	}


}
 

